﻿namespace Rsdn.Janus.NotifySound
{
    using System;
    using JetBrains.Annotations;
    using SmartApp;

    [ActivePart]
    public class NotifySoundActivePart : ActivePartBase
    {
        private readonly INotifySoundService _notifySoundService;
        private IForumsAggregatesService _forumsAggregatesService;
        private IDisposable _aggregatesChangedSubscription;
        private readonly ISynchronizer _synchronizer;
        private IDisposable _endSyncSubscription;
        private bool _wasSync = false;

        /// <summary>
        /// Initializes a new instance of the <see cref="NotifySoundActivePart"/> class.
        /// </summary>
        /// <param name="serviceProvider"></param>
        public NotifySoundActivePart([NotNull] IServiceProvider serviceProvider)
            : base(serviceProvider)
        {
            _notifySoundService = ServiceProvider.GetService<INotifySoundService>();
            _synchronizer = ServiceProvider.GetService<ISynchronizer>();
        }

        #region Overrides of ActivePartBase

        /// <summary>
        /// Core activation implementation.
        /// </summary>
        protected override void ActivateCore()
        {
            if (_notifySoundService == null)
            {
                return;
            }
            _endSyncSubscription = _synchronizer.EndSync.Subscribe(EndSync);

            // this service reread counters after getting so don't place in ctor
            _forumsAggregatesService = ServiceProvider.GetService<IForumsAggregatesService>();

            _aggregatesChangedSubscription =
                _forumsAggregatesService.AggregatesChanged.Subscribe(args => PlaySound());
        }

        /// <summary>
        /// Core passivation implementation.
        /// </summary>
        protected override void PassivateCore()
        {
            if (_aggregatesChangedSubscription != null)
            {
                _aggregatesChangedSubscription.Dispose();
                _aggregatesChangedSubscription = null;
            }

            if (_endSyncSubscription != null)
            {
                _endSyncSubscription.Dispose();
                _endSyncSubscription = null;
            }
        }

        #endregion

        /// <summary>
        /// Notification about Ends of the sync.
        /// </summary>
        /// <param name="args">The <see cref="Rsdn.Janus.EndSyncEventArgs"/> instance containing the event data.</param>
		private void EndSync(EndSyncEventArgs args)
		{
		    _wasSync = true;
		}

        /// <summary>
        /// Plays the sound.
        /// </summary>
        private void PlaySound()
        {
            if (_wasSync && _forumsAggregatesService != null && _notifySoundService != null)
            {
                _wasSync = false;

                int totalUnread = _forumsAggregatesService.UnreadMessagesCount;
                int answersMeUnread = _forumsAggregatesService.UnreadRepliesToMeCount;

                if (totalUnread > 0)
                {
                    if (answersMeUnread > 0)
                    {
                        // answers me
                        _notifySoundService.PlayAnswersMePresent();
                    }
                    else
                    {
                        // normal answers
                        _notifySoundService.PlayAnswerPresent();
                    }
                }
                else
                {
                    // Nothing new
                    _notifySoundService.PlayNoAnswers();
                }
            }
        }
    }
}